// physical-html
//
// Copyright (c)Daigo Tanaka
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

physHtml.wordObjectFactory = function(physicalHtml)
{
physHtml.particleFactory.call(this, physicalHtml);

this.createWordObjectsInElement = function(prefix, string, elementID, position, letterWidth, divHeight, style)
{
  if (string.length == 0) return;
  var wordObjectIDs = Array();
  var objects = new Array();
  var element = document.getElementById(elementID);
  if (element == null) return null;
  prevOffset = this.offset;
  this.offset = $V([0.0, -1.0]);

  var left = 0;
  for (i = 0; i < string.length; i++){
    var word = string[i];
    wordObjectIDs[i] = prefix + word + i.toFixed(0);
    var div = document.createElement("div");
    div.setAttribute('id', wordObjectIDs[i]);
    div.innerHTML = word;
    var wordLength = word.replace(/(<([^>]+)>)/ig,"").length;
    if (wordLength == 0){
      wordLength = 1;
    }
    var wordWidth =  wordLength * letterWidth;

    div.style.cssText = style + "; left: " + (position.e(physHtml.Index0) + left).toFixed(0) + "px; top: " + position.e(physHtml.Index1).toFixed(0) + "px; width:" + wordWidth + "px; height:" + divHeight + "px;";
    left += wordWidth;
    element.appendChild(div);
  }

  var newObjects = this.createOrGetObjects(wordObjectIDs);
  this.offset = prevOffset;

  return newObjects;
}

}


